package vn.ikame.android.boojtran.truyenbua;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import vn.ikame.android.boojtran.truyenbua.object.GA;
import vn.ikame.android.boojtran.truyenbua.utility.CConstant;
import vn.ikame.android.boojtran.truyenbua.utility.CStore;
import android.os.Bundle;
import android.view.Menu;

public class Splash extends SuperActivity implements Runnable {
	public static final int EXIT_DELAY = 2000;

	private ScheduledExecutorService executor;
	private boolean isPause = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		store = new CStore(this);
		try {
			showedArr = store.getShowedArr();
			storyArr = store.getStoryArr();
		} catch (Exception e) {
			e.printStackTrace();
		}
		executor = Executors.newSingleThreadScheduledExecutor();
		executor.schedule(this, EXIT_DELAY, TimeUnit.MILLISECONDS);
		GA.startGeneral(this);
		GA.trackGeneralView(APP_NAME);
		if (store.isFirstTime()) {
			GA.trackGeneralView(APP_NAME + "_installApp");
		}
		GA.stopGeneral();
		GA.startDetail(this);
		GA.trackDetailView(APP_NAME);
	}

	public void nextScreen() {
		if (!isPause) {
			executor.shutdownNow();
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					requestListItems(CConstant.SVR_IMG_LINK, 0, itemCount);
				}
			});
		}
	}

	@Override
	public void run() {
		nextScreen();
	}

	@Override
	public void onPause() {
		super.onPause();
		isPause = true;
		executor.shutdownNow();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (isPause) {
			executor = Executors.newSingleThreadScheduledExecutor();
			executor.schedule(this, EXIT_DELAY, TimeUnit.MILLISECONDS);
		}
		isPause = false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}
}
